﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AmazonProductAdvtApi;
using System.Xml.Linq;

namespace AmazonProductAdvSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        private const string MY_AWS_ACCESS_KEY_ID = "YOUR_AWS_ACCESS_KEY_ID";
        private const string MY_AWS_SECRET_KEY = "YOUR_AWS_SECRET_KEY";
        private const string DESTINATION = "ecs.amazonaws.com";
        private const string MY_ASSOCIATE_TAG = "*****-22";

        private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/2011-08-01";
        private const string ITEM_ID = "0545010225";

        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            /*
             * Here is an ItemLookup example where the request is stored as a dictionary.
             */
            var helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);
            var r1 = new Dictionary<string, String>();
            r1["Service"] = "AWSECommerceService";
            r1["Version"] = "2011-08-01";
            r1["Operation"] = "ItemLookup";
            r1["ItemId"] = ITEM_ID;
            r1["ResponseGroup"] = "Medium";
            r1["IdType"] = "EAN";
            r1["SearchIndex"] = "Books";
            r1["AssociateTag"] = MY_ASSOCIATE_TAG;  // Required from API Version 2011-08-01

            var requestUrl = helper.Sign(r1);

            var client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri(requestUrl));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var xml = XElement.Parse(e.Result);

            try
            {
                XNamespace ns = NAMESPACE;
                var item = xml.Element(ns + "Items").Element(ns + "Item");
                if (item != null)
                {
                    var titleText = item.Element(ns + "ItemAttributes").Element(ns + "Title").Value;
                    MessageBox.Show(titleText);
                }
                else
                {
                    var error = xml.Element(ns + "Items").Element(ns + "Request").Element(ns + "Errors").Element(ns + "Error");
                    if (error != null)
                    {
                        MessageBox.Show(error.Element(ns + "Message").Value);
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}